<!DOCTYPE html>
<html class="x-admin-sm" xmlns:th="http://www.thymeleleaf.org">
<head th:insert="base">
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
<div class="layui-fluid">
    <div class="layui-row">
        <form class="layui-form" action="" method="post" lay-filter = "editObj">
            <input type="hidden" name="id"/>
            <div class="layui-form-item">
                <label for="roadSwitchCode" class="layui-form-label">
                    铁路段道岔编号
                </label>
                <div class="layui-input-inline">
                    <input type="text" id="roadSwitchCode" name="roadSwitchCode" minlength="3" maxlength="20" lay-verify="range"
                           autocomplete="off" class="layui-input">
                </div>
                <div class="layui-form-mid layui-word-aux">
                    <span class="x-red">*</span>
                </div>
            </div>
            <div class="layui-form-item">
                <label for="roadSwitchModel" class="layui-form-label">
                    铁路段道岔型号
                </label>
                <div class="layui-input-inline">
                    <select id="roadSwitchModel" name="roadSwitchModel" class="valid" lay-verify="required">
                    </select>
                </div>
                <div class="layui-form-mid layui-word-aux">
                    <span class="x-red">*</span>
                </div>
            </div>

            <div class="layui-form-item">
                <label for="bureauCode" class="layui-form-label">
                    所属铁路局
                </label>
                <div class="layui-input-inline">
                    <select id="bureauCode" name="bureauCode" class="valid" lay-verify="required">
                    </select>
                </div>
                <div class="layui-form-mid layui-word-aux">
                    <span class="x-red">*</span>
                </div>
            </div>
            <div class="layui-form-item">
                <label for="lineCode" class="layui-form-label">
                    所属铁路线
                </label>
                <div class="layui-input-inline">
                    <select id="lineCode" name="lineCode" class="valid" lay-verify="required">
                    </select>
                </div>
                <div class="layui-form-mid layui-word-aux">
                    <span class="x-red">*</span>
                </div>
            </div>
            <div class="layui-form-item">
                <label for="stationCode" class="layui-form-label">
                    所属车站
                </label>
                <div class="layui-input-inline">
                    <select id="stationCode" name="stationCode" class="valid" lay-verify="required">
                    </select>
                </div>
                <div class="layui-form-mid layui-word-aux">
                    <span class="x-red">*</span>
                </div>
            </div>
            <div class="layui-form-item">
                <label for="supervisorAccount" class="layui-form-label">
                    责任监管员
                </label>
                <div class="layui-input-inline">
                    <select id="supervisorAccount" name="supervisorAccount" class="valid" lay-verify="required">
                    </select>
                </div>
                <div class="layui-form-mid layui-word-aux">
                    <span class="x-red">*</span>
                </div>
            </div>
            <div class="layui-form-item">
                <label for="repairmanAccount" class="layui-form-label">
                    责任维修员
                </label>
                <div class="layui-input-inline">
                    <select id="repairmanAccount" name="repairmanAccount" class="valid" lay-verify="required">
                    </select>
                </div>
                <div class="layui-form-mid layui-word-aux">
                    <span class="x-red">*</span>
                </div>
            </div>
            <div id="switchCodes" class="demo-transfer"></div>
            <div class="layui-form-item layui-form-text">
                <label for="remark" class="layui-form-label">备注</label>
                <div class="layui-input-block">
                    <textarea placeholder="请输入内容" id="remark" name="remark" class="layui-textarea"></textarea>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">
                </label>
                <button  class="layui-btn" lay-filter="edit" lay-submit="">
                    确认修改
                </button>
            </div>
        </form>
    </div>
</div>
<script th:inline="javascript">
    layui.use(['form','jquery','validate','tool','businessUtil','transfer'], function(){
        var form = layui.form;
        var $ = layui.jquery;
        var validate = layui.validate;
        var baseUrl = "/back/roadSwitch";
        var tool = layui.tool;
        var businessUtil = layui.businessUtil;
        var transfer = layui.transfer;
        var roadSwitch = [[${roadSwitch}]];
        init_form();


        //获取待选择的道岔数据
        var switchData = getSwitchCode(roadSwitch.switchCodeList);

        var checkData = getCheckedValue(roadSwitch.switchCodeList);


        //显示搜索框
        transfer.render({
            elem: '#switchCodes'
            ,data: switchData
            ,id:'switchCodes'
            ,title: ['待选道岔编号', '已选道岔编号']
            ,showSearch: true
            ,parseData: function(res){
                return {
                    "value": res.switchCode //数据值
                    ,"title": res.switchCode //数据标题
                    ,"disabled": res.disabled  //是否禁用
                    ,"checked": res.checked //是否选中
                }
            }
            ,value:checkData
        });


        /**
         * 初始化页面
         */
        function init_form() {
            businessUtil.createDeviceModel('roadSwitchModel','4',null,'请选择铁路段道岔型号');
            businessUtil.createBaseInfo('bureauCode','0',null,'请选择所属铁路局');
            businessUtil.createBaseInfo('lineCode','1',null,'请选择所属铁路线');
            businessUtil.createBaseInfo('stationCode','2',null,'请选择所属车站');
            businessUtil.createUserInfo('supervisorAccount','supervisor',null,'请选择责任监管员');
            businessUtil.createUserInfo('repairmanAccount','repairman',null,'请选择责任维修员');
            form.render();//重新渲染
            form.val('editObj',{
                "id":roadSwitch.id
                ,"roadSwitchCode":roadSwitch.roadSwitchCode
                ,"roadSwitchModel":roadSwitch.roadSwitchModel
                ,"bureauCode":roadSwitch.bureauCode
                ,"lineCode":roadSwitch.lineCode
                ,"stationCode":roadSwitch.stationCode
                ,"supervisorAccount":roadSwitch.supervisorAccount
                ,"repairmanAccount":roadSwitch.repairmanAccount
                ,"remark":roadSwitch.remark
            });
        }

        //添加表单校验
        form.verify(validate);

        //监听提交
        form.on('submit(edit)', function(data){
            var switchCodeList = transfer.getData('switchCodes');
            if(tool.isEmpty(switchCodeList) || switchCodeList.length == 0){
                layer.msg('请选择道岔编号', {
                        icon: msg.error,
                        time: 2000
                    },
                    null
                );
                return false;
            }
            var switchCodes = "";
            for(var i=0;i<switchCodeList.length;i++){
                switchCodes += (switchCodeList[i].value +",");
            }
            data.field.switchCodeList = switchCodes;
            var res = tool.submit(baseUrl + '/edit',data.field);
            if(res.code == return_200){
                layer.msg(res.msg, {
                        icon: msg.success,
                        time: 2000
                    },
                    function(){
                        xadmin.father_reload();
                    }
                );
            }else {
                layer.msg(res.msg, {
                        icon: msg.error,
                        time: 2000
                    },
                    function(){
                        xadmin.father_reload();
                    }
                );
            }
            return false;
        });

        /**
         * 获取道岔信息
         */
        function getSwitchCode(switchCodeList){
            var switchInfoData = null;
            $.ajax({
                url:'/back/switchInfo/getAllList'
                ,type:'post'
                ,data:{
                    runType:'0'
                    ,switchCodes:switchCodeList
                }
                ,async:false
                ,dataType:'json'
                ,success:function(res){
                    if (res.code === return_200) {
                        switchInfoData = res.data;
                    } else {
                        console.log("获取道岔编号失败");
                    }
                }
                ,error:function(){
                    console.log("获取道岔编号异常");
                }
            });
            return switchInfoData;
        }
        
        function getCheckedValue(switchCodes) {
            return switchCodes.split(",");
        }
    });
</script>
</body>
</html>